package cn.com.sd.common.util;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author zhanghaiwei
 *
 */
public class Page {

	private int totalRows; //总行数
	private int pageSize = 10; //每页显示的行数
	private int currentPage; //当前页号
	private int totalPages; //总页数
	private int startRow; //当前页在数据库中的起始行
	
	public Page() {}

	public Page(Integer totalRows) {
		this.totalRows = totalRows;
		this.totalPages = totalRows / pageSize;
		int mod = totalRows % pageSize;
		if (mod > 0) {
			totalPages++;
		}
		currentPage = 1;
		startRow = 0;
	}
	
	public Page(Integer totalRows, Integer currentPage) {
		if (currentPage == null || currentPage <= 0) {
			currentPage = 1;
		}
		this.totalRows = totalRows;
		this.totalPages = totalRows / pageSize;
		int mod = totalRows % pageSize;
		if (mod > 0) {
			totalPages++;
		}
		
		if (currentPage <= 0) {
			currentPage = 1;
			startRow = 0;
		} else if (currentPage >= totalPages) {
			currentPage = totalPages;
			startRow = (currentPage - 1) * pageSize;
		} else {
			startRow = (currentPage - 1) * pageSize;
		}
		this.currentPage = currentPage;
	}
	
	public Page(Integer totalRows, Integer currentPage, Integer pageSize) {
		if (currentPage == null) {
			currentPage = 1;
		}
		this.totalRows = totalRows;
		this.totalPages = totalRows / pageSize;
		int mod = totalRows % pageSize;
		if (mod > 0) {
			totalPages++;
		}
		
		if (currentPage <= 0) {
			currentPage = 1;
			startRow = 0;
		} else if (currentPage >= totalPages) {
			currentPage = totalPages;
			startRow = (currentPage - 1) * pageSize;
		} else {
			startRow = (currentPage - 1) * pageSize;
		}
		this.currentPage = currentPage;
	}
	
	/**
	 * 判断是否还有下一页
	 * @return
	 */
	public Boolean isEndRow() {
		if (currentPage * pageSize > totalRows) {
			return false;
		}
		return true;
	}
	
	public void first() {
		currentPage = 1;
		startRow = 0;
	}

	public void previous() {
		if (currentPage == 1) {
			return;
		}
		currentPage--;
		startRow = (currentPage - 1) * pageSize;
	}

	public void next() {
		if (currentPage < totalPages) {
			currentPage++;
		}
		startRow = (currentPage - 1) * pageSize;
	}

	public void last() {
		currentPage = totalPages > 0 ? totalPages : 1;
		startRow = (currentPage - 1) * pageSize;
	}
	
	public Page method(String method) {
		if (StringUtils.isEmpty(method)) {
			method = "first";
		}
		if (method.equals("first")) {
			first();
		} else if (method.equals("previous")) {
			previous();
		} else if (method.equals("next")) {
			next();
		} else if (method.equals("last")) {
			last();
		}
		return this;
	}
	
	/**
	 * @return the totalRows
	 */
	public int getTotalRows() {
		return totalRows;
	}
	/**
	 * @param totalRows the totalRows to set
	 */
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}
	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		if (currentPage >= totalPages) {
			return totalPages;
		}
		return currentPage;
	}
	/**
	 * @param currentPage the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		if (currentPage <= 0) {
			currentPage = 1;
		}
		this.currentPage = currentPage;
	}
	/**
	 * @return the totalPages
	 */
	public int getTotalPages() {
		return totalPages;
	}
	/**
	 * @param totalPages the totalPages to set
	 */
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	/**
	 * @return the startRow
	 */
	public int getStartRow() {
		return startRow;
	}
	/**
	 * @param startRow the startRow to set
	 */
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	
	
}
